package org.pay.service.impl;

import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.pay.core.common.constant.MchConstant;
import org.pay.core.entity.MchTradeOrder;
import org.pay.core.entity.MchTradeOrderExample;
import org.pay.core.service.IMchTradeOrderService;
import org.pay.service.dao.mapper.MchTradeOrderMapper;

import java.util.List;

/**
 * @author: yangshancheng
 * @date: 17/12/21
 * @description:
 */
@Service
public class MchTradeOrderServiceImpl implements IMchTradeOrderService {

    @Autowired
    private MchTradeOrderMapper mchTradeOrderMapper;

    @Override
    public List<MchTradeOrder> select(int pageIndex, int pageSize, MchTradeOrder mchTradeOrder) {
        MchTradeOrderExample example = new MchTradeOrderExample();
        example.setOrderByClause("create_time DESC");
        example.setOffset(pageIndex);
        example.setLimit(pageSize);
        MchTradeOrderExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, mchTradeOrder);
        return mchTradeOrderMapper.selectByExample(example);
    }

    @Override
    public int count(MchTradeOrder mchTradeOrder) {
        MchTradeOrderExample example = new MchTradeOrderExample();
        MchTradeOrderExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, mchTradeOrder);
        return mchTradeOrderMapper.countByExample(example);
    }

    @Override
    public int add(MchTradeOrder mchTradeOrder) {
        return mchTradeOrderMapper.insertSelective(mchTradeOrder);
    }

    @Override
    public MchTradeOrder findByTradeOrderId(String tradeOrderId) {
        return mchTradeOrderMapper.selectByPrimaryKey(tradeOrderId);
    }

    @Override
    public MchTradeOrder findByMchIdAndTradeOrderId(Long mchId, String tradeOrderId) {
        MchTradeOrderExample example = new MchTradeOrderExample();
        MchTradeOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchIdEqualTo(mchId);
        criteria.andTradeOrderIdEqualTo(tradeOrderId);
        List<MchTradeOrder> mchTradeOrderList = mchTradeOrderMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(mchTradeOrderList)) return null;
        return mchTradeOrderList.get(0);
    }

    @Override
    public MchTradeOrder findByMchIdAndPayOrderId(Long mchId, String payOrderId) {
        MchTradeOrderExample example = new MchTradeOrderExample();
        MchTradeOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchIdEqualTo(mchId);
        criteria.andPayOrderIdEqualTo(payOrderId);
        List<MchTradeOrder> mchTradeOrderList = mchTradeOrderMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(mchTradeOrderList)) return null;
        return mchTradeOrderList.get(0);
    }


    @Override
    public int updateStatus4Success(String tradeOrderId) {
        MchTradeOrder mchTradeOrder = new MchTradeOrder();
        mchTradeOrder.setState(MchConstant.TRADE_ORDER_STATUS_SUCCESS);
        MchTradeOrderExample example = new MchTradeOrderExample();
        MchTradeOrderExample.Criteria criteria = example.createCriteria();
        criteria.andTradeOrderIdEqualTo(tradeOrderId);
        criteria.andStateEqualTo(MchConstant.TRADE_ORDER_STATUS_INIT);
        return mchTradeOrderMapper.updateByExampleSelective(mchTradeOrder, example);
    }

    @Override
    public int updateStatus4Complete(String tradeOrderId) {
        MchTradeOrder mchTradeOrder = new MchTradeOrder();
        mchTradeOrder.setState(MchConstant.TRADE_ORDER_STATUS_COMPLETE);
        MchTradeOrderExample example = new MchTradeOrderExample();
        MchTradeOrderExample.Criteria criteria = example.createCriteria();
        criteria.andTradeOrderIdEqualTo(tradeOrderId);
        criteria.andStateEqualTo(MchConstant.TRADE_ORDER_STATUS_SUCCESS);
        return mchTradeOrderMapper.updateByExampleSelective(mchTradeOrder, example);
    }

    @Override
    public int update(MchTradeOrder mchTradeOrder) {
        return mchTradeOrderMapper.updateByPrimaryKeySelective(mchTradeOrder);
    }

    void setCriteria(MchTradeOrderExample.Criteria criteria, MchTradeOrder mchTradeOrder) {
        if(mchTradeOrder != null) {
            if(mchTradeOrder.getMchId() != null) criteria.andMchIdEqualTo(mchTradeOrder.getMchId());
            if(StringUtils.isNotBlank(mchTradeOrder.getAppId())) criteria.andAppIdEqualTo(mchTradeOrder.getAppId());
            if(StringUtils.isNotBlank(mchTradeOrder.getTradeOrderId())) criteria.andTradeOrderIdEqualTo(mchTradeOrder.getTradeOrderId());
            if(mchTradeOrder.getState() != null && mchTradeOrder.getState() != -99) criteria.andStateEqualTo(mchTradeOrder.getState());
            if(StringUtils.isNotBlank(mchTradeOrder.getPayOrderId())) criteria.andPayOrderIdEqualTo(mchTradeOrder.getPayOrderId());
        }
    }

}
